Street Coder by Sedat Kapanoglu
Author:Sedat Kapanoglu [Sedat Kapanoglu]
Language: eng
Format: epub, mobi
Publisher: Manning Publications
Published: 2022-01-17T16:00:00+00:00
int
32-bit signed
-2147483648..2147483647
Duh
uint
32-bit unsigned
0..4294967295
No
long
64-bit signed
-9223372036854775808..9223372036854775807
No
ulong
64-bit unsigned
0..18446744073709551615
No
short
16-bit signed
-32768..32767
Yes
ushort
16-bit unsigned
0..65535
Yes
sbyte
8-bit signed
-128..127
Yes
byte
8-bit unsigned
0..255
Yes
When you use an unsigned type, trying to pass a negative constant value to your function will cause a compiler error. Passing a variable with a negative value is possible only with explicit type casting, which makes you think about whether the value you have is really suitable for that function at the call site. Itâs not the functionâs responsibility to validate for negative arguments anymore. Assume that a function needs to return trending tags in your microblogging website up to only a specified number of tags. It receives a number of items to retrieve rows of posts, as in listing 4.10.
Also in listing 4.10, a GetTrendingTags function returns items by taking the number of items into account. Notice that the input value is a byte instead of int because we donât have any use case more than 255 items in the trending tag list. That actually immediately eliminates the cases where an input value can be negative or too large. We donât even need to validate the input anymore. This results in one fewer test case and a much better range of input values, which reduces the area for bugs immediately.
Listing 4.10 Receiving posts only belonging to a certain page
using System; using System.Collections.Generic; using System.Linq; namespace Posts { public class Tag { public Guid Id { get; set; } public string Title { get; set; } } public class PostService { public const int MaxPageSize = 100; private readonly IPostRepository db; public PostService(IPostRepository db) { this.db = db; } public IList<Tag> GetTrendingTags(byte numberOfItems) { â¶ return db.GetTrendingTagTable() .Take(numberOfItems) â· .ToList(); } } }
â¶ We chose byte instead of int.
â· A byte or a ushort can be passed as safely as int too.
Two things are happening here. First, we chose a smaller data type for our use case. We donât intend to support billions of rows in a trending tag box. We donât even know what that would look like. We have narrowed down our input space. Second, we chose byte, an unsigned type, which cannot be negative. That way, we avoided a possible test case and a potential problem that might cause an exception. LINQâs Take function doesnât throw an exception with a List, but it can when it gets translated to a query for a database like Microsoft SQL Server. By changing the type, we avoid those cases, and we donât need to write tests for them.
Note that .NET uses int as the de facto standard type for many operations like indexing and counting. Opting for a different type might require you to cast and convert values into ints if you happen to interact with standard .NET components. You need to make sure that youâre not digging yourself into a hole by being pedantic. Your quality of life and the enjoyment you get from writing code are more important than a certain one-off case youâre trying to avoid. For example, if you need more than 255 items in the future, youâll have to replace all references to bytes with shorts or ints, which can be time consuming.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Exploring Deepfakes by Bryan Lyon and Matt Tora(7717)
Robo-Advisor with Python by Aki Ranin(7616)
Offensive Shellcode from Scratch by Rishalin Pillay(6099)
Microsoft 365 and SharePoint Online Cookbook by Gaurav Mahajan Sudeep Ghatak Nate Chamberlain Scott Brewster(5014)
Ego Is the Enemy by Ryan Holiday(4956)
Management Strategies for the Cloud Revolution: How Cloud Computing Is Transforming Business and Why You Can't Afford to Be Left Behind by Charles Babcock(4438)
Python for ArcGIS Pro by Silas Toms Bill Parker(4177)
Elevating React Web Development with Gatsby by Samuel Larsen-Disney(3882)
Machine Learning at Scale with H2O by Gregory Keys | David Whiting(3618)
Learning C# by Developing Games with Unity 2021 by Harrison Ferrone(3285)
Speed Up Your Python with Rust by Maxwell Flitton(3231)
Liar's Poker by Michael Lewis(3221)
OPNsense Beginner to Professional by Julio Cesar Bueno de Camargo(3195)
Extreme DAX by Michiel Rozema & Henk Vlootman(3171)
Agile Security Operations by Hinne Hettema(3122)
Linux Command Line and Shell Scripting Techniques by Vedran Dakic and Jasmin Redzepagic(3108)
Essential Cryptography for JavaScript Developers by Alessandro Segala(3083)
Cryptography Algorithms by Massimo Bertaccini(3001)
AI-Powered Commerce by Andy Pandharikar & Frederik Bussler(2982)
